package com.example.userconsumer.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
 * @author likaiping
 * @createTime 2024/7/7 15:16
 */
@Configuration
//@EnableWebSocketMessageBroker
@EnableWebSocket
public class WebSocketConfig /*implements WebSocketMessageBrokerConfigurer*/ {

/*    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        // 配置消息代理 设置应用程序目的地前缀为 /app，所有发送到控制器的消息都将具有 /app 作为前缀
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // 注册一个STOMP端点，并指定端点的URL
//        registry.addEndpoint("/websocketSeckill").setAllowedOriginPatterns("*").withSockJS();
        registry.addEndpoint("/websocketSeckill").setAllowedOrigins("http://localhost:5173").withSockJS();
    }*/

    /**
     * 注入一个ServerEndpointExporter,该Bean会自动注册使用@ServerEndpoint注解申明的websocket endpoint
     * 这个bean的注册,用于扫描带有@ServerEndpoint的注解成为websocket
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}
